﻿using Devonline.AspNetCore;
using Devonline.Identity.Admin.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;

namespace Devonline.Identity.Admin.Controllers
{
    /// <summary>
    /// 组织部门管理
    /// </summary>    
    [Route("api/[controller]")]
    [SecurityHeaders]
    [ApiController]
    [Authorize(Roles = AppSettings.GROUP_MAINTAINERS)]
    public class GroupsController : ControllerBase
    {
        private readonly string _userName;
        private readonly ILogger<GroupsController> _logger;
        private readonly IDataWithAttachmentService<IdentityDbContext, Group> _dataService;

        public GroupsController(ILogger<GroupsController> logger, IDataWithAttachmentService<IdentityDbContext, Group> dataService)
        {
            _userName = User.Identity.Name;
            _logger = logger;
            _dataService = dataService;
        }

        /// <summary>
        /// 查询组织单位
        /// </summary>
        /// <returns></returns>
        [HttpGet, EnableQuery]
        public IActionResult Get()
        {
            _logger.LogInformation("user {user} query the group list", _userName);
            return Ok(_dataService.GetQueryable());
        }

        /// <summary>
        /// 创建组织单位
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> CreateAsync(GroupViewModel viewModel)
        {
            _logger.LogInformation("user {user} will add the group {group}", _userName, viewModel.Name);

            try
            {
                var group = await _dataService.AddAsync(viewModel);
                _logger.LogWarning("user {user} success to add the group {group}", _userName, viewModel.Name);
                return Ok(group);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "user {user} add the group {group} throw exception", _userName, viewModel.Name);
            }

            return BadRequest($"创建组织单位 {viewModel.Name} 没有成功!");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> ChangeAsync(GroupViewModel viewModel)
        {
            _logger.LogInformation("user {user} will change the group {group}", _userName, viewModel.Name);

            try
            {
                var group = await _dataService.UpdateAsync(viewModel);
                _logger.LogWarning("user {user} success to add the group {group}", _userName, viewModel.Name);
                return Ok(group);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "user {user} add the group {group} throw exception", _userName, viewModel.Name);
            }

            return BadRequest($"修改组织单位 {viewModel.Name} 没有成功!");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteAsync(string id)
        {
            try
            {
                _logger.LogInformation("user {user} will delete the group {group}", _userName, id);
                await _dataService.DeleteAsync(id);
                _logger.LogWarning("user {user} success to delete the group {group}", _userName, id);
                return Ok();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "user {user} remove the group {group} throw exception", _userName, id);
            }

            return BadRequest($"删除组织单位 {id} 没有成功!");
        }
    }
}
